<template>
	<view>
		<!-- 标题栏 -->
		<view class="topBox">
			<head-navbar :back="false" class="headNav">
				<view class="">分类</view>
			</head-navbar>
		</view>
		<!-- <view class="header" style="background-color: #F2F2F2;margin: 0 auto;" :style="'top:'+ navHeight">
			<view class="top">
				<view class="searchBox">
					<image src="../../static/img/searchicon.png" mode=""></image>
					<input type="text" placeholder="女装 冬季 长款" :value="proNameCur" />
				</view>
			</view>
		</view> -->
		<view class="category-list" id="category">
			<!-- 左侧分类导航 -->
			<scroll-view scroll-y="true" class="left" :style="{height: height + 'px',marginTop: fixtop+'px'}">
				<!-- <scroll-view scroll-y="true" class="left"> -->
				<view class="row" v-for="(category, index) in typeList" :key="index" :class="[index == showCategoryIndex ? 'on' : '']"
				 @click="showCategory(index)">
					<view class="text">
						<view class="block"></view>
						{{ category.goodsTypeName }}
					</view>
				</view>
			</scroll-view>
			<!-- 右侧子导航 -->
			<!-- scroll-view scroll-y="true" class="right" style="margin-top: 100upx;"> -->
			<scroll-view scroll-y="true" class="right" :style="{height: height + 'px',marginTop: fixtop+'px'}"
			 :refresher-enabled="true" @refresherrefresh="refresherrefresh" :refresher-triggered="triggered"
			 refresher-background="#f6f6f6" :refresher-threshold="60"  @refresherrestore="onRestore">
				<view class="category" v-for="(category, index) in typeList" :key="index" v-show="index == showCategoryIndex">
					<view v-for="(box, i) in category.subTypeList" :key="i" style="margin-bottom: 30upx;">
						<view>
							<text style="color: #222222;padding-right: 8px;padding-left: 8px">{{ box.goodsTypeName }}</text>
						</view>
						<view class="list" v-if="box.subTypeList.length>0">
							<view class="box" v-for="(item, i1) in box.subTypeList" :key="i1">
								<image @click="toPage(0,'/pages/category/goods?keyword='+item.goodsTypeName+'&goodsTypeId='+item.goodsTypeId)"
								 :src="item.goodsTypeImg" lazy-load="https://www.gomyorder.cn/logo.png"></image>
								<view class="text">{{ item.goodsTypeName }}</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>
<script>
	import {
		goodsTypeList
	} from '@/api/goods/index'
	export default {
		data() {
			return {
				navHeight: '',
				height: '',
				proNameCur: '',
				showCategoryIndex: 0,
				headerPosition: 'fixed',
				typeList: [],
				fixtop: 0,
				triggered: true,
				freshing: false
			};
		},
		onReady() {
			let that = this
			uni.getSystemInfo({
				success: function(res) { // res - 各种参数
					let info = uni.createSelectorQuery().select(".topBox");
					info.boundingClientRect(function(data) { //data - 各种参数
						that.height = res.windowHeight - data.height
						that.fixtop = data.height
					}).exec()
				}
			});
		},
		onPageScroll(e) {
			//兼容iOS端下拉时顶部漂移
			if (e.scrollTop >= 0) {
				this.headerPosition = 'fixed';
			} else {
				this.height = e.scrollTop;
				this.headerPosition = 'absolute';
			}
		},
		// onPullDownRefresh() {
		// 	this.getGoodsType()
		//        setTimeout(function () {
		//            uni.stopPullDownRefresh();
		//        }, 1000);
		//    },
		onLoad() {
			this.getGoodsType()
		},
		methods: {
			refresherrefresh() {
				if (this.freshing) return;
				this.freshing = true;
				var that = this
				this.getGoodsType()
				setTimeout(() => {
					this.triggered = false;
					this.freshing = false;
				}, 1000)
			},
			onRestore() {
				this.triggered = 'restore'; // 需要重置
			},
			getGoodsType() {
				uni.showLoading({
					title: '加载中'
				})
				goodsTypeList().then(res => {
					this.typeList = res.data.obj
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
				})
			},
			//分类切换显示
			showCategory(index) {
				this.showCategoryIndex = index;
			},
			toCategory(name) {
				uni.navigateTo({
					url: '/pages/categray/search?cid=' + this.showCategoryIndex + '&name=' + name
				});
			}
		}
	};
</script>
<style lang="scss">
	@import './index.scss';

	.status {
		width: 100%;
		height: 0;
		position: fixed;
		z-index: 10;
		background-color: #fff;
		top: 0;
		/*  #ifdef  APP-PLUS  */
		height: var(--status-bar-height); //覆盖样式
		/*  #endif  */
	}

	.header {
		width: 100%;
		padding: 0 4%;
		height: 100upx;
		display: flex;
		align-items: center;
		position: fixed;
		top: 0;
		z-index: 10;
		background-color: #fff;
		/*  #ifdef  APP-PLUS  */
		top: var(--status-bar-height);
		/*  #endif  */

		.addr {
			width: 120upx;
			height: 60upx;
			flex-shrink: 0;
			display: flex;
			align-items: center;
			font-size: 28upx;

			.icon {
				height: 60upx;
				margin-right: 5upx;
				display: flex;
				align-items: center;
				font-size: 42upx;
				color: #ffc50a;
			}
		}

		.input-box {
			width: 100%;
			height: 60upx;
			background-color: #f5f5f5;
			border-radius: 30upx;
			position: relative;
			display: flex;
			align-items: center;

			.icon {
				display: flex;
				align-items: center;
				position: absolute;
				top: 0;
				right: 0;
				width: 60upx;
				height: 60upx;
				font-size: 34upx;
				color: #c0c0c0;
			}

			input {
				padding-left: 28upx;
				height: 28upx;
				font-size: 28upx;
			}
		}

		.icon-btn {
			width: 60upx;
			height: 60upx;
			flex-shrink: 0;
			display: flex;

			.icon {
				width: 60upx;
				height: 60upx;
				display: flex;
				justify-content: flex-end;
				align-items: center;
				font-size: 42upx;
			}
		}
	}

	.place {
		background-color: #ffffff;
		height: 100upx;
		/*  #ifdef  APP-PLUS  */
		margin-top: var(--status-bar-height);
		/*  #endif  */
	}

	.category-list {
		width: 100%;
		background-color: #f2f2f2;
		display: flex;

		.left,
		.right {
			top: 0upx;
			bottom: 0upx;
		}

		.left {
			position: fixed;
			width: 24%;
			left: 0upx;
			background-color: #f2f2f2;

			.row {
				width: 100%;
				height: 90upx;
				display: flex;
				align-items: center;
				background-color: #FFFFFF;

				.text {
					width: 100%;
					position: relative;
					font-size: 28upx;
					display: flex;
					justify-content: center;
					color: #3c3c3c;

					.block {
						position: absolute;
						width: 0upx;
						left: 0;
					}
				}

				&.on {
					height: 100upx;
					background-color: #f2f2f2;

					.text {
						font-size: 30upx;
						font-weight: 600;
						color: #2d2d2d;

						.block {
							width: 10upx;
							height: 80%;
							top: 10%;
							background-color: $base-color;
						}
					}
				}
			}
		}

		.right {
			position: fixed;
			width: 76%;
			left: 24%;

			.category {
				width: 98%;
				padding: 20upx 3%;

				.banner {
					width: 100%;
					height: 24.262vw;
					border-radius: 10upx;
					overflow: hidden;
					box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.3);

					image {
						width: 100%;
						height: 24.262vw;
					}
				}

				.list {
					margin-top: 20upx;
					width: 100%;
					height: 100%;
					display: flex;
					flex-wrap: wrap;
					padding-bottom: 20upx;
					border-radius: 20upx;
					background-color: #FFFFFF;

					.box {
						width: calc(68.44vw / 3);
						margin-top: 30upx;
						display: flex;
						justify-content: center;
						align-items: center;
						flex-wrap: wrap;

						image {
							width: 140upx;
							height: 140upx;
						}

						.text {
							margin-top: 5upx;
							width: 100%;
							display: flex;
							justify-content: center;
							font-size: 26upx;
						}
					}
				}
			}
		}
	}
</style>
